<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class photosModule extends StoreBaseModule
{
	public function index(){
		$id = intval($_REQUEST['id']);
		$GLOBALS['tmpl']->caching = true;
		$cache_id  = md5(MODULE_NAME.ACTION_NAME.$id);	
		if (!$GLOBALS['tmpl']->is_cached('store_photos.html', $cache_id))	
		{
			$store_info = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."supplier_location where id = ".$id);
			if(!$store_info)
			{
				showErr($GLOBALS['lang']['NO_STORE_INFO']);
			}
			$site_nav[] = array('name'=>$GLOBALS['lang']['HOME_PAGE'],'url'=>url("store","index"));
			$site_nav[] = array('name'=>$store_info['name'],'url'=>url("store","view",array("id"=>$store_info['id'])));
			$site_nav[] = array('name'=>"餐厅相册",'url'=>url("store","photos",array("id"=>$store_info['id'])));
			$GLOBALS['tmpl']->assign("site_nav",$site_nav);
			$GLOBALS['tmpl']->assign("store_info",$store_info);
		}
		$GLOBALS['tmpl']->display("store_photos.html",$cache_id);	
	}
	
	function load_store_photo_list($attr){
		$store_info=$attr['store_info'];
		
		//获取当前图片
 		$extWhere ="";
 		$extImgType ="";
 		if(intval($_REQUEST['pid']))
 			$extWhere = " and id=".intval($_REQUEST['pid']) ;
 		
 		if(isset($_REQUEST['images_group_id']))
 			$extImgType = " and images_group_id=".intval($_REQUEST['images_group_id']);
 		
 			
 		$sql = "select id,image,click_count,images_group_id,brief,user_id,create_time from ".DB_PREFIX."supplier_location_images where status = 1 and supplier_location_id=".intval($store_info['id'])." $extWhere $extImgType order by sort desc, id desc";
 		$pic_info = $GLOBALS['db']->getRow($sql);
 		
 		if(intval($pic_info['user_id'])> 0)
 			$pic_info['user_name'] = $GLOBALS['db']->getOne("select account_name from ".DB_PREFIX."supplier_account where id=".intval($pic_info['user_id']));

 		//更新统计
 		if(check_ipop_limit(get_client_ip(),"store_photos",10,$pic_info['id']))
		{
			$GLOBALS['db']->query("update  ".DB_PREFIX."supplier_location_images set click_count=click_count+1 where id=".intval($pic_info['id']));
 			$pic_info['click_count'] +=  1 ;
		}
 		
 		//上一张
 		$prev_pic = $GLOBALS['db']->getOne("select  min(id) from ".DB_PREFIX."supplier_location_images where status = 1 and supplier_location_id=".intval($store_info['id'])." and id > ".intval($pic_info['id'])." $extImgType order by sort desc, id desc");
 		$GLOBALS['tmpl']->assign("prev_pic",$prev_pic);
 		//下一张
 		$next_pic = $GLOBALS['db']->getOne("select  max(id) from ".DB_PREFIX."supplier_location_images where status = 1 and supplier_location_id=".intval($store_info['id'])." and id < ".intval($pic_info['id'])." $extImgType order by sort desc, id desc");
 		$GLOBALS['tmpl']->assign("next_pic",$next_pic);
 		
 		//获取全部图片数
 		$total_image =   $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location_images where status = 1 and supplier_location_id=".intval($store_info['id'])."");
 		$GLOBALS['tmpl']->assign("total_image",$total_image);
 		//获取店铺分类图片
 		$images_group_list = load_auto_cache("store_image_group_list",array("cate_id"=>intval($store_info['deal_cate_id'])));

 		foreach($images_group_list as $k=>$v){
	 		$images_group_list[$k]['count'] = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."supplier_location_images where status = 1 and images_group_id=".intval($v['id'])." and supplier_location_id=".intval($store_info['id'])."");
	 	}	 	
 		$GLOBALS['tmpl']->assign("images_group_list",$images_group_list);
 		
 		//获取分类商户图片
 		$pic_list = $GLOBALS['db']->getAll("select id,image,click_count,images_group_id,brief,user_id,create_time from ".DB_PREFIX."supplier_location_images where status = 1 and supplier_location_id=".intval($store_info['id'])." $extImgType order by sort desc, id desc");
 		
 		$pic_idx  = 1;
 		$pic_tmp_idx = 1;
 		foreach($pic_list as $k => $v)
 		{
 			if($v['id'] == $pic_info['id']){
 				$pic_idx = $pic_tmp_idx;
 			}
 			$pic_tmp_idx ++;
 		}
 		
		$GLOBALS['tmpl']->assign("store_info",$store_info);
		$GLOBALS['tmpl']->assign("pic_idx",$pic_idx);
		$GLOBALS['tmpl']->assign("pic_info",$pic_info);
		$GLOBALS['tmpl']->assign("pic_list",$pic_list);
		$GLOBALS['tmpl']->assign("images_group_id",intval($_REQUEST['images_group_id']));
		return $GLOBALS['tmpl']->fetch("inc/store_photo_list.html");	
	}

}
?>